Event.observe ( window, 'load', bootstrap );

function bootstrap () {
  new Ajax.Request ( '/rest/products/radiator', {
    method         : 'get',
    requestHeaders : { Accept : 'application/json' },
    onSuccess      : function ( response ) {
      var html = products_table ( response.responseJSON );
      
      if ( html ) {
        $( 'products' ).replace ( html );
      }
    },
    onFailure      : function ( response ) {
      display_error ( response.responseJSON.error );
    }
  } );
}

function products_table ( response ) {
  var html =
    "<table id=\"products\" class=\"products\">\n"
  + "  <thead>\n"
  + "    <tr>\n"
  + "      <th>ID</th>\n"
  + "      <th>Stage</th>\n"
  + "      <th>Spec. ID</th>\n"
  + "      <th>Die ID</th>\n"
  + "      <th>CI When</th>\n"
  + "      <th>Last Harvest</th>\n"
  + "      <th>Action</th>\n"
  + "    </tr>\n"
  + "  </thead>\n"
  + "  <tfoot>\n"
  + "    <tr>\n"
  + "      <th>ID</th>\n"
  + "      <th>Stage</th>\n"
  + "      <th>Spec. ID</th>\n"
  + "      <th>Die ID</th>\n"
  + "      <th>CI When</th>\n"
  + "      <th>Last Harvest</th>\n"
  + "      <th>Action</th>\n"
  + "    </tr>\n"
  + "  </tfoot>\n"
  + "  <tbody>\n"
  ;

  var i = 1;
  
  response.each ( function ( h ) {
    if ( h.status_id == 'done' ) {
      var last_tag;
      
      if ( h.specification_tags ) {
        var tags = h.specification_tags.findAll ( is_tools ).sort ().reverse ();
        
        last_tag = tags[0];
        
        if ( last_tag ) {
          last_tag = last_tag.replace ( /^TOOLS-/, '' );
        }
      }
      
      html = html
      + "    <tr class=\"" + ( i % 2 ? "odd" : "even" ) + "\">\n"
      + "      <td>" +   h.id                        + "</td>\n"
      + "      <td>" + ( h.stage_id         || '-' ) + "</td>\n"
      + "      <td>" +   h.specification_id          + "</td>\n"
      + "      <td>" + ( h.die_id           || '-' ) + "</td>\n"
      + "      <td>" +   h.last_ci                   + "</td>\n"
      + "      <td>" + (   last_tag         || '-' ) + "</td>\n"
      + "      <td><input type=\"button\" id=\"" + h.specification_id + "\" name=\"" + h.specification_id + "\" value=\"Harvest\" class=\"harvest\" onclick=\"harvest('" + h.specification_id + "');\"></td>\n"
      + "    </tr>\n"
      ;

      i = i + 1;
    }
  } );

  html = html
  + "  </tbody>\n"
  + "</table>\n"
  ;
  
  return html;
}

function harvest ( specification_id ) {
  new Ajax.Request ( '/rest/specification/' + specification_id + '/milestone/TOOLS?dummy=1', {
    method         : 'put',
    requestHeaders : { Accept : 'application/json' },
    onCreate       : function () {
      display_progress ( 'Harvesting...' );
    },
    onSuccess      : function ( response ) {
      hide_progress ();
      
      show_popup ( null, response.responseJSON.message, null, null, null, response.responseJSON.tarball );
    },
    onFailure      : function ( response ) {
      hide_progress ();
      
      display_error ( response.responseJSON.error );
    }
  } );
}
